package admin

import (
	"github.com/gin-gonic/gin"
	"go_my_framework/app/core/session"
	"go_my_framework/app/global/request"
	"go_my_framework/app/service"
	"go_my_framework/util/base"
	"go_my_framework/util/sli"
	"strconv"
)

// 文章列表
func ArticleIndex(c *gin.Context) {
	if c.Request.Method == "POST" {
		var f request.ArticleForm
		if err := c.ShouldBind(&f); err != nil {
			base.Error(c, err.Error(), nil)
			return
		}
		svc := service.ArticleService{}

		list, count, err := svc.GetList(f)
		if err != nil {
			base.Error(c, err.Error(), nil)
			return
		}
		data := make(map[string]interface{})
		data["data"] = list
		data["count"] = count
		base.Success(c, "", data)
	} else {
		title := c.Query("title")
		base.Html(c, "/admin/article/index", gin.H{"title": title})
	}
}

// 添加文章
func ArticleAdd(c *gin.Context) {
	if c.Request.Method == "POST" {
		var f request.ArticleAddForm
		if err := c.ShouldBind(&f); err != nil {
			base.Error(c, err.Error(), nil)
			return
		}
		if len(f.CategoryId) == 0 {
			base.Error(c, "请选择分类", nil)
			return
		}
		// 添加文章
		svc := service.ArticleService{}
		adminId := session.Get(c, "admin_id").(int)
		id, err := svc.Add(f.Title, f.Desc, f.Content, f.CoverImage, f.IsOpenComment, f.IsTop, f.IsRecommended, adminId)
		if err != nil {
			base.Error(c, err.Error(), nil)
		} else {
			// 添加分类
			AcSvc := service.ArticleCategoryService{}
			for _, v := range f.CategoryId {
				_ = AcSvc.Add(v, id)
			}
			base.Success(c, "添加成功", nil)
		}
	} else {
		var f request.CategoryForm
		cateSvc := service.CategoryService{}
		category, _, _ := cateSvc.GetList(f)
		base.Html(c, "/admin/article/add", gin.H{"category": category})
	}
}

// 编辑文章
func ArticleEdit(c *gin.Context) {
	if c.Request.Method == "POST" {
		var f request.ArticleAddForm
		if err := c.ShouldBind(&f); err != nil {
			base.Error(c, err.Error(), nil)
			return
		}
		if len(f.CategoryId) == 0 {
			base.Error(c, "请选择分类", nil)
			return
		}

		idStr := c.PostForm("id")
		id, _ := strconv.Atoi(idStr)

		// 更新文章
		adminId := session.Get(c, "admin_id").(int)
		svc := service.ArticleService{}
		err := svc.Edit(id, f.Title, f.Desc, f.Content, f.CoverImage, f.IsOpenComment, f.IsTop, f.IsRecommended, adminId)
		if err != nil {
			base.Error(c, err.Error(), nil)
		} else {
			// 分类
			AcSvc := service.ArticleCategoryService{}
			AcSvc.UpdateOrAdd(id, f.CategoryId)
			base.Success(c, "更新成功", nil)
		}
	} else {
		// 文章
		idStr := c.Query("id")
		id, _ := strconv.Atoi(idStr)
		svc := service.ArticleService{}
		articleInfo := svc.GetRow(id, "")
		// 分类
		ArtCateSvc := service.ArticleCategoryService{}
		cate := ArtCateSvc.GetCateByArticleId(id)

		var f request.CategoryForm
		cateSvc := service.CategoryService{}
		categoryList, _, _ := cateSvc.GetList(f)
		category := make([]map[string]interface{}, len(categoryList), len(categoryList))
		for k, v := range categoryList {
			tmp := make(map[string]interface{})
			tmp["id"] = v.Id
			tmp["name"] = v.Name
			if sli.InArray(v.Id, cate) {
				tmp["checked"] = true
			} else {
				tmp["checked"] = false
			}
			category[k] = tmp
		}
		base.Html(c, "/admin/article/edit", gin.H{"category": category, "data": articleInfo})
	}
}

// 删除文章
func ArticleDel(c *gin.Context) {
	idStr := c.PostForm("id")
	id, _ := strconv.Atoi(idStr)
	svc := service.ArticleService{}
	_ = svc.Del(id)
	base.Success(c, "已删除", nil)
}
